﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NS_QuangCaoTiepThiBUS;

namespace NS_QuangCaoTiepThi.QCTT.User
{
    public partial class NoiDungChuaDuyet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                LayDanhSachNoiDungChuaDuyetTheoNgay();
            }
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            /*if (txtNgayTu.Text == string.Empty && txtNgayDen.Text == string.Empty)
            {
                LayDanhSachNoiDungChuaDuyet();
            }*/

            if (txtNgayTu.Text != string.Empty && txtNgayDen.Text == string.Empty)
            {
                Alert.Show("Ngay den khong duoc rong do da chon Ngay tu");

                gvNoiDungChuaDuyet.DataSource = null;
                gvNoiDungChuaDuyet.DataBind();

                return;
            }

            if (txtNgayDen.Text != string.Empty && txtNgayTu.Text == string.Empty)
            {
                Alert.Show("Ngay tu khong duoc rong do da chon Ngay Den");

                gvNoiDungChuaDuyet.DataSource = null;
                gvNoiDungChuaDuyet.DataBind();

                return;
            }

            if (txtNgayTu.Text != string.Empty && txtNgayDen.Text != string.Empty)
            {
                LayDanhSachNoiDungChuaDuyetTheoNgay();
            }
        }

        protected void gvNoiDungChuaDuyet_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvNoiDungChuaDuyet.EditIndex = e.NewEditIndex;

            LayDanhSachNoiDungChuaDuyetTheoNgay();
        }

        protected void gvNoiDungChuaDuyet_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtNoiDungTinTucChung = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNoiDungTinTucChung"));
            TextBox txtNgayBatDauNoiDungTinTucChung = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayBatDauNoiDungTinTucChung"));
            TextBox txtNgayKetThucNoiDungTinTucChung = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayKetThucNoiDungTinTucChung"));
            TextBox txtNoiDungTinTucNoiBat = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNoiDungTinTucNoiBat"));
            TextBox txtNgayBatDauNoiDungTinTucNoiBat = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayBatDauNoiDungTinTucNoiBat"));
            TextBox txtNgayKetThucNoiDungTinTucNoiBat = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayKetThucNoiDungTinTucNoiBat"));
            FileUpload fupdHinhAnhQuangCaoDung = (FileUpload)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("fupdHinhAnhQuangCaoDung"));
            HiddenField hfHinhAnhQuangCaoDung = (HiddenField)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("hfHinhAnhQuangCaoDung"));
            TextBox txtNgayBatDauHinhAnhQuangCaoDung = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayBatDauHinhAnhQuangCaoDung"));
            TextBox txtNgayKetThucHinhAnhQuangCaoDung = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayKetThucHinhAnhQuangCaoDung"));
            FileUpload fupdHinhAnhQuangCaoNgang = (FileUpload)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("fupdHinhAnhQuangCaoNgang"));
            HiddenField hfHinhAnhQuangCaoNgang = (HiddenField)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("hfHinhAnhQuangCaoNgang"));
            TextBox txtNgayBatDauHinhAnhQuangCaoNgang = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayBatDauHinhAnhQuangCaoNgang"));
            TextBox txtNgayKetThucHinhAnhQuangCaoNgang = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayKetThucHinhAnhQuangCaoNgang"));
            FileUpload fupdVideoNoiBo = (FileUpload)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("fupdVideoNoiBo"));
            HiddenField hfVideoNoiBo = (HiddenField)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("hfVideoNoiBo"));
            TextBox txtNgayBatDauVideoNoiBo = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayBatDauVideoNoiBo"));
            TextBox txtNgayKetThucVideoNoiBo = (TextBox)(gvNoiDungChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtNgayKetThucVideoNoiBo"));

            //Kiem tra
            if (txtNoiDungTinTucChung.Text != string.Empty && txtNgayBatDauNoiDungTinTucChung.Text == string.Empty)
            {
                Alert.Show("Ngay bat dau noi dung tin tuc chung khong duoc rong");

                return;
            }

            if (txtNoiDungTinTucNoiBat.Text != string.Empty && txtNgayBatDauNoiDungTinTucNoiBat.Text == string.Empty)
            {
                Alert.Show("Ngay bat dau noi dung tin tuc noi bat khong duoc rong");

                return;
            }

            if (fupdHinhAnhQuangCaoDung.PostedFile.FileName != string.Empty && txtNgayBatDauHinhAnhQuangCaoDung.Text == string.Empty)
            {
                Alert.Show("Ngay bat dau hinh anh noi bo khong duoc rong");

                return;
            }

            if (fupdHinhAnhQuangCaoNgang.PostedFile.FileName != string.Empty && txtNgayBatDauHinhAnhQuangCaoNgang.Text == string.Empty)
            {
                Alert.Show("Ngay bat dau hinh anh khach hang khong duoc rong");

                return;
            }

            if (fupdVideoNoiBo.PostedFile.FileName != string.Empty && txtNgayBatDauVideoNoiBo.Text == string.Empty)
            {
                Alert.Show("Ngay bat dau video khong duoc rong");

                return;
            }

            if (txtNgayKetThucNoiDungTinTucChung.Text != string.Empty && txtNgayBatDauNoiDungTinTucChung.Text != string.Empty)
            {
                DateTime ngayKetThuc = Convert.ToDateTime(txtNgayKetThucNoiDungTinTucChung.Text.Substring(0,10)).Date;
                DateTime ngayBatDau = Convert.ToDateTime(txtNgayBatDauNoiDungTinTucChung.Text.Substring(0, 10)).Date;

                if (ngayKetThuc < ngayBatDau)
                {
                    Alert.Show("Ngay ket thuc khong duoc nho hon Ngay bat dau");

                    return;
                }
            }

            if (txtNgayKetThucNoiDungTinTucNoiBat.Text != string.Empty && txtNgayBatDauNoiDungTinTucNoiBat.Text != string.Empty)
            {
                DateTime ngayKetThuc = Convert.ToDateTime(txtNgayKetThucNoiDungTinTucNoiBat.Text.Substring(0, 10)).Date;
                DateTime ngayBatDau = Convert.ToDateTime(txtNgayBatDauNoiDungTinTucNoiBat.Text.Substring(0, 10)).Date;

                if (ngayKetThuc < ngayBatDau)
                {
                    Alert.Show("Ngay ket thuc khong duoc nho hon Ngay bat dau");

                    return;
                }
            }

            if (txtNgayKetThucHinhAnhQuangCaoDung.Text != string.Empty && txtNgayKetThucHinhAnhQuangCaoDung.Text != string.Empty)
            {
                DateTime ngayKetThuc = Convert.ToDateTime(txtNgayKetThucHinhAnhQuangCaoDung.Text.Substring(0, 10)).Date;
                DateTime ngayBatDau = Convert.ToDateTime(txtNgayKetThucHinhAnhQuangCaoDung.Text.Substring(0, 10)).Date;

                if (ngayKetThuc < ngayBatDau)
                {
                    Alert.Show("Ngay ket thuc khong duoc nho hon Ngay bat dau");

                    return;
                }
            }

            if (txtNgayKetThucHinhAnhQuangCaoNgang.Text != string.Empty && txtNgayKetThucHinhAnhQuangCaoNgang.Text != string.Empty)
            {
                DateTime ngayKetThuc = Convert.ToDateTime(txtNgayKetThucHinhAnhQuangCaoNgang.Text.Substring(0, 10)).Date;
                DateTime ngayBatDau = Convert.ToDateTime(txtNgayKetThucHinhAnhQuangCaoNgang.Text.Substring(0, 10)).Date;

                if (ngayKetThuc < ngayBatDau)
                {
                    Alert.Show("Ngay ket thuc khong duoc nho hon Ngay bat dau");

                    return;
                }
            }

            if (txtNgayKetThucVideoNoiBo.Text != string.Empty && txtNgayKetThucVideoNoiBo.Text != string.Empty)
            {
                DateTime ngayKetThuc = Convert.ToDateTime(txtNgayKetThucVideoNoiBo.Text.Substring(0, 10)).Date;
                DateTime ngayBatDau = Convert.ToDateTime(txtNgayKetThucVideoNoiBo.Text.Substring(0, 10)).Date;

                if (ngayKetThuc < ngayBatDau)
                {
                    Alert.Show("Ngay ket thuc khong duoc nho hon Ngay bat dau");

                    return;
                }
            }

            //Sua
            NoiDung ndDTO = new NoiDung();

            ndDTO.MaNoiDung = gvNoiDungChuaDuyet.DataKeys[e.RowIndex].Value.ToString();

            ndDTO.NoiDungTinTucChung = txtNoiDungTinTucChung.Text;

            if (txtNgayBatDauNoiDungTinTucChung.Text != string.Empty)
            {
                ndDTO.NgayBatDauTinTucChung = DateTime.ParseExact(txtNgayBatDauNoiDungTinTucChung.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayBatDauTinTucChung = null;
            }

            if (txtNgayKetThucNoiDungTinTucChung.Text != string.Empty)
            {
                ndDTO.NgayKetThucTinTucChung = DateTime.ParseExact(txtNgayKetThucNoiDungTinTucChung.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayKetThucTinTucChung = null;
            }

            ndDTO.NoiDungTinTucNoiBat = txtNoiDungTinTucNoiBat.Text;

            if (txtNgayBatDauNoiDungTinTucNoiBat.Text != string.Empty)
            {
                ndDTO.NgayBatDauTinTucNoiBat = DateTime.ParseExact(txtNgayBatDauNoiDungTinTucNoiBat.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayBatDauTinTucNoiBat = null;
            }

            if (txtNgayKetThucNoiDungTinTucNoiBat.Text != string.Empty)
            {
                ndDTO.NgayKetThucTinTucNoiBat = DateTime.ParseExact(txtNgayKetThucNoiDungTinTucNoiBat.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayKetThucTinTucNoiBat = null;
            }

            ndDTO.TenFileHinhAnhNoiBo = fupdHinhAnhQuangCaoDung.PostedFile.FileName;

            if (txtNgayBatDauHinhAnhQuangCaoDung.Text != string.Empty)
            {
                ndDTO.NgayBatDauHinhAnhNoiBo = DateTime.ParseExact(txtNgayBatDauHinhAnhQuangCaoDung.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayBatDauHinhAnhNoiBo = null;
            }

            if (txtNgayKetThucHinhAnhQuangCaoDung.Text != string.Empty)
            {
                ndDTO.NgayKetThucHinhAnhNoiBo = DateTime.ParseExact(txtNgayKetThucHinhAnhQuangCaoDung.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayKetThucHinhAnhNoiBo = null;
            }

            ndDTO.TenFileHinhAnhKhachHang = fupdHinhAnhQuangCaoNgang.PostedFile.FileName;

            if (txtNgayBatDauHinhAnhQuangCaoNgang.Text != string.Empty)
            {
                ndDTO.NgayBatDauHinhAnhKhachHang = DateTime.ParseExact(txtNgayBatDauHinhAnhQuangCaoNgang.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayBatDauHinhAnhKhachHang = null;
            }

            if (txtNgayKetThucHinhAnhQuangCaoNgang.Text != string.Empty)
            {
                ndDTO.NgayKetThucHinhAnhKhachHang = DateTime.ParseExact(txtNgayKetThucHinhAnhQuangCaoNgang.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayKetThucHinhAnhKhachHang = null;
            }

            ndDTO.TenFileVideoNoiBo = fupdVideoNoiBo.PostedFile.FileName;

            if (txtNgayBatDauVideoNoiBo.Text != string.Empty)
            {
                ndDTO.NgayBatDauVideoNoiBo = DateTime.ParseExact(txtNgayBatDauVideoNoiBo.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayBatDauVideoNoiBo = null;
            }

            if (txtNgayKetThucVideoNoiBo.Text != string.Empty)
            {
                ndDTO.NgayKetThucVideoNoiBo = DateTime.ParseExact(txtNgayKetThucVideoNoiBo.Text.Substring(0, 10), "dd/MM/yyyy", null);
            }
            else
            {
                ndDTO.NgayKetThucVideoNoiBo = null;
            }

            ndDTO.MaNguoiSoanThao = (String)Session["MaNguoiDung"];

            NoiDungBUS ndBUS = new NoiDungBUS();
            ndBUS.Sua(ndDTO);

            if (fupdHinhAnhQuangCaoDung.PostedFile.FileName != string.Empty)
            {
                UploadImageFile(fupdHinhAnhQuangCaoDung);
            }

            if (fupdHinhAnhQuangCaoNgang.PostedFile.FileName != string.Empty)
            {
                UploadImageFile(fupdHinhAnhQuangCaoNgang);
            }

            if (fupdVideoNoiBo.PostedFile.FileName != string.Empty)
            {
                UploadVideoFile(fupdVideoNoiBo);
            }

            Alert.Show("Da sua thanh cong");

            gvNoiDungChuaDuyet.EditIndex = -1;

            LayDanhSachNoiDungChuaDuyetTheoNgay();
        }

        protected void gvNoiDungChuaDuyet_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvNoiDungChuaDuyet.EditIndex = -1;

            LayDanhSachNoiDungChuaDuyetTheoNgay();
        }

        protected void gvNoiDungChuaDuyet_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            NoiDung ndDTO = new NoiDung();
            ndDTO.MaNoiDung = gvNoiDungChuaDuyet.DataKeys[e.RowIndex].Value.ToString();

            NoiDungBUS ndBUS = new NoiDungBUS();
            ndBUS.Xoa(ndDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachNoiDungChuaDuyetTheoNgay();
        }

        protected void gvNoiDungChuaDuyet_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNoiDungChuaDuyet.PageIndex = e.NewPageIndex;

            LayDanhSachNoiDungChuaDuyetTheoNgay();
        }

        protected void LayDanhSachNoiDungChuaDuyet()
        {
            NoiDungBUS ndBUS = new NoiDungBUS();
            gvNoiDungChuaDuyet.DataSource = ndBUS.LayDanhSachNoiDungChuaDuyet();
            gvNoiDungChuaDuyet.DataBind();
        }

        protected void LayDanhSachNoiDungChuaDuyetTheoNgay()
        {
            if (txtNgayTu.Text != string.Empty && txtNgayDen.Text != string.Empty)
            {
                DateTime ngayTu = Convert.ToDateTime(txtNgayTu.Text);
                DateTime ngayDen = Convert.ToDateTime(txtNgayDen.Text);

                NoiDungBUS ndBUS = new NoiDungBUS();
                gvNoiDungChuaDuyet.DataSource = ndBUS.LayDanhSachNoiDungChuaDuyetTheoNgay(ngayTu, ngayDen);
                gvNoiDungChuaDuyet.DataBind();
            }
        }

        protected void UploadImageFile(FileUpload fileUpload)
        {
            string path = Server.MapPath("..//Images");

            path += "/" + fileUpload.PostedFile.FileName;

            fileUpload.PostedFile.SaveAs(path);
        }

        protected void UploadVideoFile(FileUpload fileUpload)
        {
            string path = Server.MapPath("..//Videos");

            path += "/" + fileUpload.PostedFile.FileName;

            fileUpload.PostedFile.SaveAs(path);
        }
    }
}